package com.gexiaobao.pigeon.viewmodel.request

import androidx.lifecycle.MutableLiveData
import com.gexiaobao.pigeon.app.eventViewModel
import com.gexiaobao.pigeon.app.model.bean.FeatherColorResponse
import com.gexiaobao.pigeon.app.model.bean.OrganizerDeviceResponse
import com.gexiaobao.pigeon.app.model.bean.UiState
import com.gexiaobao.pigeon.app.model.param.RingInfoParams
import com.gexiaobao.pigeon.app.network.ListDataUiState
import com.gexiaobao.pigeon.app.network.apiService
import com.gexiaobao.pigeon.app.util.RxToast
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.ext.request

/**
 * @Author      : hxw
 * @Date        : 2023/3/3 9:20
 * @Describe    :
 */
class RequestAddPigeonViewModel : BaseViewModel() {

    private var pageNo: Int = 1
    private var pageSize: Int = 999999

    /** 获取组织方所有设备 */
    val deviceResult: MutableLiveData<OrganizerDeviceResponse> = MutableLiveData()

    //添加和修改赛鸽
    var createPigeon: MutableLiveData<UiState> = MutableLiveData()

    //羽色列表
    var featherColor: MutableLiveData<ListDataUiState<FeatherColorResponse.ColorList>> = MutableLiveData()

    //组织方修改赛鸽
    var orgUpdatePigeonResult: MutableLiveData<UiState> = MutableLiveData()

    /** 获取组织方所有设备 */
    fun getGatherDeviceList() {
        request({ apiService.getGatherDeviceList(pageNo, pageSize) }, {
            //请求成功
            deviceResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    fun createPigeon(bean: RingInfoParams) {
        request({ apiService.createPigeon(bean) }, {
            val createResult = UiState(isSuccess = true)
            createPigeon.value = createResult
            //请求成功 已自动处理了 请求结果是否正常
            eventViewModel.toRefreshList.value = true
        }, {
            //请求失败 网络异常，或者请求结果码错误都会回调在这里
            val createResult = it.errorLog?.let { it1 -> UiState(isSuccess = false, errorMsg = it1) }
            createPigeon.value = createResult
        })
    }

    /** 获取羽色列表 */
    fun getFeatherColor() {
        request({ apiService.getFeatherColor() }, {
            val listDataUiState = ListDataUiState(
                isSuccess = true, isEmpty = it.isEmpty(), listData = it.list
            )
            featherColor.value = listDataUiState
        }, {
            val listDataUiState = ListDataUiState(isSuccess = false, errMessage = it.msg, listData = arrayListOf<FeatherColorResponse.ColorList>())
            featherColor.value = listDataUiState
        })
    }

    /** 组织方修改赛鸽 */
    fun orgUpdatePigeon(body: RingInfoParams) {
        request({ apiService.orgUpdatePigeon(body) }, {
            val result = UiState(isSuccess = true)
            orgUpdatePigeonResult.value = result
        }, {
            val result = UiState(isSuccess = false, errorMsg = it.msg)
            orgUpdatePigeonResult.value = result
        })
    }

}